﻿/*----------------------------------------------------------------
// 公司名称：公司名称
// 
// 产品名称：Gaf
//
// 创 建 人：netcasewqs
// 创建日期：2022/3/4
// 邮    箱：netcasewqs@qq.com
//
// Copyright (C) 2022 Gaf，保留所有权利。
//
//----------------------------------------------------------------*/
namespace Gaf.Impl;

class LiteDelegateActivator : DelegateActivatorBase<LiteActivatorDescriptor>
{
    internal AddinDescriptor AddinDescriptor { get; }
    public LiteDelegateActivator(LiteActivatorDescriptor activatorDescriptor) : base(activatorDescriptor, AssemblyLoadContext.Default.Assemblies, null)
    {
        AddinDescriptor = new AddinDescriptor();

        var instance = activatorDescriptor.Attribute ?? Instance;

        try
        {
            AddinDescriptor.Name = (string)(activatorDescriptor.GetNameMethod!.Invoke(instance, null))!;
        }
        catch (TargetInvocationException ex)
        {
            throw ex.InnerException!;
        }

        try
        {
            var value = (activatorDescriptor.GetStartLevelMethod!.Invoke(instance, null))!;
            AddinDescriptor.StartLevel = value switch
            {
                uint => (uint)value,
                int => (uint)((int)value),
                ushort => (uint)(ushort)value,
                short => (uint)((short)value),
                _ => 0,
            };
        }
        catch (TargetInvocationException ex)
        {
            throw ex.InnerException!;
        }
    }
}

